package com.oa.modules.approvalsec1.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.oa.modules.approvalsec1.entity.Operationlog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * (Operationlog)表数据库访问层
 *
 * @author makejava
 * @since 2020-11-20 11:23:58
 */
@Mapper
public interface OperationlogDao extends BaseMapper<Operationlog> {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Operationlog queryById(Long id);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<Operationlog> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param operationlog 实例对象
     * @return 对象列表
     */
    List<Operationlog> queryAll(Operationlog operationlog);

    /**
     * 新增数据
     *
     * @param operationlog 实例对象
     * @return 影响行数
     */
    int insert(Operationlog operationlog);

    /**
     * 修改数据
     *
     * @param operationlog 实例对象
     * @return 影响行数
     */
    int update(Operationlog operationlog);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Long id);

    /**
     * 查询总条数
     *
     * @param params
     * @return
     */
    @Select("<script>" +
            "SELECT COUNT(1) AS total FROM operationlog " +
            "where 1 = 1 " +
            "<when test='params.operation!=null and params.operation != \"\"'> and operationlog.operation LIKE CONCAT('%',#{params.operation},'%') </when>" +
            "<when test='params.userName!=null and params.userName != \"\"'> and operationlog.user_name LIKE CONCAT('%',#{params.userName},'%') </when>" +
            "<when test='params.startDate!=null and params.endDate!=null'> and DATE_FORMAT(operationlog.operation_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{params.startDate},'%Y-%m-%d') AND DATE_FORMAT(#{params.endDate},'%Y-%m-%d') </when>" +
            "</script>")
    long findPage(@Param("params") Map<String, Object> params);

    /**
     * 查询分页数据
     *
     * @param page
     * @param params
     * @return
     */
    @Select("<script>" +
            "select " +
            "operationlog.* " +
            "from operationlog " +
            "where 1 = 1 " +
            "<when test='params.operation!=null and params.operation != \"\"'> and operationlog.operation LIKE CONCAT('%',#{params.operation},'%') </when>" +
            "<when test='params.userName!=null and params.userName != \"\"'> and operationlog.user_name LIKE CONCAT('%',#{params.userName},'%') </when>" +
            "<when test='params.startDate!=null and params.endDate!=null'> and DATE_FORMAT(operationlog.operation_time,'%Y-%m-%d') BETWEEN DATE_FORMAT(#{params.startDate},'%Y-%m-%d') AND DATE_FORMAT(#{params.endDate},'%Y-%m-%d') </when>" +
            "order by operationlog.operation_time desc"+
            "</script>")
    List<Operationlog> findRecords(@Param("page") IPage<Operationlog> page, @Param("params") Map<String, Object> params);
}
